package flc.ast.activity;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;

import android.os.Bundle;
import android.view.View;

import com.bumptech.glide.Glide;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.google.gson.Gson;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import flc.ast.AppConfig;
import flc.ast.BaseAc;
import flc.ast.R;
import flc.ast.adapter.HotAdapter;
import flc.ast.databinding.ActivityMovieDetailBinding;
import flc.ast.utils.AppSpUtils;
import stark.common.api.StkResApi;
import stark.common.base.IReqRetCallback;
import stark.common.basic.base.BaseWebviewActivity;
import stark.common.basic.event.EventStatProxy;
import stark.common.basic.utils.IntentUtil;
import stark.common.bean.StkResBean;

public class MovieDetailActivity extends BaseAc<ActivityMovieDetailBinding> {

    public static StkResBean stkResBean;
    private HotAdapter likeAdapter;
    private int num;
    private boolean isLike;
    private boolean isCollect;
    private List<StkResBean> listSpLike;
    private List<StkResBean> listSpCollect;

    @Override
    protected int onCreate() {
        return R.layout.activity_movie_detail;
    }

    @Override
    protected void initView() {
        EventStatProxy.getInstance().statEvent1(this, mDataBinding.event1Container);

        mDataBinding.ivMovieDetailBack.setOnClickListener(v -> onBackPressed());

        mDataBinding.ivDetail.setOnClickListener(this);
        mDataBinding.ivLike.setOnClickListener(this);
        mDataBinding.ivCollect.setOnClickListener(this);
        mDataBinding.ivShare.setOnClickListener(this);

        mDataBinding.rvLikeList.setLayoutManager(new LinearLayoutManager(mContext, LinearLayoutManager.HORIZONTAL, false));
        likeAdapter = new HotAdapter();
        mDataBinding.rvLikeList.setAdapter(likeAdapter);
        likeAdapter.setOnItemClickListener(this);
    }

    @Override
    protected void onClickCallback(View v) {
        switch (v.getId()) {
            case R.id.ivDetail://查看详情
                BaseWebviewActivity.open(mContext, stkResBean.getUrl(), stkResBean.getName());
                break;
            case R.id.ivLike://点赞
                if (isLike) {
                    listSpLike.remove(stkResBean);
                    AppSpUtils.saveLikeList(listSpLike);
                    isLike = false;
                    num--;
                    mDataBinding.tvLikeNum.setText(getString(R.string.like_num, num));
                    mDataBinding.ivLike.setImageResource(R.drawable.adianzan);
                } else {
                    List<StkResBean> listSave = new ArrayList<>();
                    listSave.add(stkResBean);
                    if (listSpLike != null && listSpLike.size() != 0) {
                        listSpLike.addAll(listSave);
                        AppSpUtils.saveLikeList(listSpLike);
                    } else {
                        AppSpUtils.saveLikeList(listSave);
                    }
                    isLike = true;
                    num++;
                    mDataBinding.tvLikeNum.setText(getString(R.string.like_num, num));
                    mDataBinding.ivLike.setImageResource(R.drawable.ayidianz);
                }
                break;
            case R.id.ivCollect://收藏
                if (isCollect) {
                    listSpCollect.remove(stkResBean);
                    AppSpUtils.saveCollectList(listSpCollect);
                    isCollect = false;
                    num--;
                    mDataBinding.ivCollect.setImageResource(R.drawable.ashoucang);
                } else {
                    List<StkResBean> listSave = new ArrayList<>();
                    listSave.add(stkResBean);
                    if (listSpCollect != null && listSpCollect.size() != 0) {
                        listSpCollect.addAll(listSave);
                        AppSpUtils.saveCollectList(listSpCollect);
                    } else {
                        AppSpUtils.saveCollectList(listSave);
                    }
                    isCollect = true;
                    num++;
                    mDataBinding.ivCollect.setImageResource(R.drawable.ayishouc);
                }
                break;
            case R.id.ivShare://分享
                IntentUtil.shareText(mContext, stkResBean.getUrl());
                break;
        }
    }

    @Override
    protected void onItemClickCallback(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
        StkResBean bean = (StkResBean) adapter.getItem(position);
        MovieDetailActivity.stkResBean = bean;
        startActivity(MovieDetailActivity.class);
        finish();
    }

    @Override
    protected void initData() {
        getData();
        Glide.with(mContext).load(stkResBean.getThumbUrl()).into(mDataBinding.ivDetailImg);
        mDataBinding.tvDetailName.setText(stkResBean.getName());
        mDataBinding.tvDetailDesc.setText(stkResBean.getDesc());

        String str = new Gson().toJson(stkResBean.getExtra());
        String director = getString(R.string.director);
        String actor = getString(R.string.actor);
        try {
            JSONObject extra = new JSONObject(str);
            JSONArray actorList = extra.optJSONArray("actorList");
            if (actorList != null) {
                for (int i = 0; i < actorList.length(); i++) {
                    JSONObject item = actorList.getJSONObject(i);
                    if (item.getString("role").equals("导演")) {
                        director += item.getString("name") + " ";
                    } else {
                        actor += item.getString("name") + " ";
                    }
                }
            }
        } catch (JSONException e) {
            throw new RuntimeException(e);
        }
        mDataBinding.tvDetailDirector.setText(director);
        mDataBinding.tvDetailActor.setText(actor);

        //评分
        double score = (double) stkResBean.getScore_total() / (stkResBean.getScore_count() == 0 ? 1 : stkResBean.getScore_count());
        String formattedNumber = String.format("%.1f", score);
        mDataBinding.tvDetailScore.setText(formattedNumber);
        if (score > 8) {
            mDataBinding.zzratingbar.setRating(5);
        } else if (score > 6) {
            mDataBinding.zzratingbar.setRating(4);
        } else if (score > 4) {
            mDataBinding.zzratingbar.setRating(3);
        } else if (score > 2) {
            mDataBinding.zzratingbar.setRating(2);
        } else {
            mDataBinding.zzratingbar.setRating(1);
        }

        Random random = new Random();
        num = 1000 + random.nextInt(1000);
        mDataBinding.tvLikeNum.setText(getString(R.string.like_num, num));

        setLikeData();
        setCollectData();

    }

    private void getData() {
        StkResApi.getTagResourceList(this, AppConfig.LIKE_DATA, StkResApi.createParamMap(0, 5), false, new IReqRetCallback<List<StkResBean>>() {
            @Override
            public void onResult(boolean success, String msg, @Nullable List<StkResBean> result) {
                if (success) {
                    if (result != null && result.size() > 0) {
                        likeAdapter.setList(result);
                    }
                }
            }
        });
    }

    //点赞
    private void setLikeData() {
        listSpLike = AppSpUtils.getLikeList();
        isLike = false;
        mDataBinding.ivLike.setImageResource(R.drawable.adianzan);
        if (listSpLike != null && listSpLike.size() != 0) {
            for (StkResBean bean : listSpLike) {
                if (bean.equals(stkResBean)) {
                    isLike = true;
                    mDataBinding.ivLike.setImageResource(R.drawable.ayidianz);
                    break;
                }
            }
        }
    }

    //收藏
    private void setCollectData() {
        listSpCollect = AppSpUtils.getCollectList();
        isCollect = false;
        mDataBinding.ivCollect.setImageResource(R.drawable.ashoucang);
        if (listSpCollect != null && listSpCollect.size() != 0) {
            for (StkResBean bean : listSpCollect) {
                if (bean.equals(stkResBean)) {
                    isCollect = true;
                    mDataBinding.ivCollect.setImageResource(R.drawable.ayishouc);
                    break;
                }
            }
        }
    }

}